package com.tm.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.util.Properties;

public class DruidUtil {

    private static DruidDataSource dataSource = null;

    private static Env p = Env.getInstance();

    static {
        try {
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(p);

            dataSource.setUrl(p.getProperty("url"));
            dataSource.setUsername(p.getProperty("user"));
            dataSource.setPassword(p.getProperty("pass"));
            dataSource.setDriverClassName(p.getProperty("driver"));

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取数据源对象
     * @return 数据库连接的数据源对象
     */
    public static DataSource getDataSource(){
        return dataSource;
    }
}
